/**
 * 
 */
package com.roc.util.constant;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.dao.ISQLBaseDao;

/**
 * @author Administrator
 * 
 */
@Component
@Transactional(readOnly = true)
public class ConfigPropertiesUtils {
	static Logger logger = LogManager.getLogger(ConfigPropertiesUtils.class);

	private final static String propertiesKey = "KEY";
	private final static String propertiesValue = "VALUE";

	@Resource(name = "sqlBaseDao")
	private ISQLBaseDao sqlBaseDao;

	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	private void loadPropertiesFromDB(final String propertiesName,
			final String sql, final Map target) {
		List<Map<String, Object>> queryResult = null;
		try {
			queryResult = sqlBaseDao.querySqlToMap(sql);
		} catch (DaoException e) {
			logger.error(sql);
			logger.error("加载" + propertiesName + "信息失败");
		}

		if (queryResult != null) {
			logger.info("清空原" + propertiesName + "信息[" + target.size() + "]");
			target.clear();
			for (Map<String, Object> map : queryResult) {
				target.put(String.valueOf(map.get(propertiesKey)),
						String.valueOf(map.get(propertiesValue)));
				logger.debug("加载"+propertiesName+":"+map.get(propertiesKey)+"="+map.get(propertiesValue));
			}
			logger.info("数据库加载" + propertiesName + "信息[" + queryResult.size()+ "]");
		}
	}
	 
	private void loadPropertiesListDB(final String propertiesName,
			final String sql, final List<Object> object) {
		List<Object> queryResult = null;
		try {
			queryResult = sqlBaseDao.queryAll(sql);
		} catch (DaoException e) {
			logger.error(sql);
			logger.error("加载" + propertiesName + "信息失败");
		}

		if (queryResult != null) {
			logger.info("清空原" + propertiesName + "信息[" + object.size() + "]");
			object.clear();
			for (Object obj : queryResult) {
				
				object.add(obj);
				//logger.debug("加载"+propertiesName+":"+map.get(propertiesKey)+"="+map.get(propertiesValue));
			}
			 
			logger.info("数据库加载" + propertiesName + "信息[" +object.size()+ "]");
		}
	}

	public void loadSystemDbProperties() {
		loadPropertiesFromDB("系统配置",
				"SELECT p.key,p.value FROM t_properties p",
				ConfigProperties.systemProperties);
	}
	public void loadAgentTypeProperties() {
		loadPropertiesFromDB("代理商类型",
				"SELECT p.level_code  key,p.code_name value FROM t_agent_type p",
				ConfigProperties.agentTypeProperties);
	}
	public void loadRoleTypeProperties() {
		loadPropertiesFromDB("角色",
				"SELECT p.role_code  key,p.role_name value FROM t_role p where p.role_code<>'00'",
				ConfigProperties.roleTypeProperties);
	}
	public void loadHYMccTypeProperties() {
		loadPropertiesFromDB("MCC分类",
				"SELECT p.code KEY,p.name VALUE FROM TBL_UNIONPAY_MCC p",
				ConfigProperties.hyMccTypeProperties);
	}
	public void loadYJMccTypeProperties() {
		loadPropertiesFromDB("MCC分类",
				"SELECT p.mcc_type_code KEY,p.mcc_type_name VALUE FROM t_mcc_type p",
				ConfigProperties.mccTypeProperties);
	}
	public  void loadGBProvieProperties() {
		loadPropertiesFromDB("加载GB省份",
				" SELECT T.DIC_CODE KEY, T.NAME VALUE FROM TBL_DICTIONARYLAYERITEM T WHERE T.DIC_TYPE = 'GB_Area_MAP'  AND T.DIC_LEVEL = '0' ORDER BY T.DIC_CODE  ASC",
				ConfigProperties.gbProviceProperties);
	}
	public  void loadProvieProperties() {
		loadPropertiesFromDB("加载省份",
				" SELECT T.DIC_CODE KEY, T.NAME  VALUE FROM TBL_DICTIONARYLAYERITEM T WHERE T.DIC_TYPE = 'UnionPay_Area'  AND T.DIC_LEVEL = '1' ORDER BY T.DIC_CODE  ASC",
				ConfigProperties.unionProviceProperties);
	}
	public  void loadCityProperties() {
		loadPropertiesFromDB("加载城市",
				" SELECT T.DIC_CODE KEY , T.NAME VALUE FROM TBL_DICTIONARYLAYERITEM T WHERE T.DIC_TYPE = 'UnionPay_Area'  AND T.DIC_LEVEL = '2' ORDER BY T.DIC_CODE  ASC",
				ConfigProperties.unionCityProperties);
	}
	public  void loadAreaCodeProperties() {
		loadPropertiesFromDB("加载地区",
				" SELECT T.DIC_CODE KEY, T.NAME VALUE FROM TBL_DICTIONARYLAYERITEM T WHERE T.DIC_TYPE = 'UnionPay_Area'  AND T.DIC_LEVEL = '1' ORDER BY T.DIC_CODE  ASC",
				ConfigProperties.unionAreaProperties);
	}
		
	
	public  void loadTransTypeProperties() {
		loadPropertiesFromDB("交易类型",
				"SELECT p.id KEY,p.tran_name VALUE FROM BC_TRAN_TYPE p where p.tran_code IN('0820002','0800003','020031','0500201','0320207','020000','020020','040000','0320201') order by p.id ",
				ConfigProperties.tranTypeProperties);
	}
	/*public  void loadBankListProperties() {
		loadPropertiesFromDB("银行",
				" SELECT c.BANKID  key,c. bankname value  FROM t_bank_info C  ORDER BY c.bankname ",
				ConfigProperties.bankListProperties);
	}*/
	/*public void loadCardBinProperties(){
		loadPropertiesListDB("卡BIN",
				"  SELECT cardBin,cardName,bankName  FROM(SELECT t.cardbin,LENGTH(t.cardbin) a,t.cardlen ,cardno,cardname,bankname  FROM etb_syscardbinno t) GROUP BY a,cardlen,cardbin,cardname,bankname ORDER BY a desc  ",
				ConfigProperties.cardBinListProperties);
	}*/
	public void loadSysDBProperties(){
		loadPropertiesListDB("加载数据字典",
				"  SELECT DIC_TYPE,DIC_CODE,DIC_NAME,DISP_ORDER FROM TBL_DICTIONARYITEM ORDER BY DIC_CODE",
				ConfigProperties.sysDbListProperties);
	}
	public void loadFirstSysDBProperties(){
		loadPropertiesListDB("加载Layer数据字典",
				"  SELECT DIC_CODE,NAME,DIC_TYPE,DIC_LEVEL,DIC_PCODE,DISP_ORDER FROM TBL_DICTIONARYLAYERITEM T  ",
				ConfigProperties.layerDBListProperties);
		//处理数据字典
		ConfigProperties.handleFirstSysDB();
	}

	public void loadYsbBrandProperties() {
		loadPropertiesFromDB("云商宝品牌",
				"SELECT p.dic_code KEY,p.dic_name VALUE FROM t_brand_sn p",
				ConfigProperties.ysbBrandProperties);
	}
	public void loadYsbModelProperties() {
		loadPropertiesListDB("云商宝型号",
				"SELECT p.dic_pcode P_KEY , p.dic_code KEY,p.dic_name VALUE FROM t_model_sn p",
				ConfigProperties.ysbModelProperties);

	}
	
	@SuppressWarnings("unused")
	public void loadAllProperties() {
		//loadSystemDbProperties();
		//加载代理商类别
		loadAgentTypeProperties();
		//加载角色
		loadRoleTypeProperties();
		//加载MCC分类码
		loadYJMccTypeProperties();
		loadHYMccTypeProperties();
		//加载地区
		loadAreaCodeProperties();
		/*//加载省份
		loadGBProvieProperties();
		//加载省份
		loadProvieProperties();
		//加载城市
		loadCityProperties();
		*/
		//加载交易类型
		loadTransTypeProperties(); 
		/*//加载银行
		loadBankListProperties();
		//加载卡BIN
		loadCardBinProperties();*/
		//加载数据字典
		loadSysDBProperties();
		
		//加载数据字典2
		loadFirstSysDBProperties();
		
		//加载云商宝品牌
		loadYsbBrandProperties();
		//加载云商宝型号
		loadYsbModelProperties();
		
	}
}
